package com.earnings.okhttputils.utils.ui.dialog.phoenix.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * DrawHook * Created by Zane on 2015/3/4.
 *
 * modify by LK on 2016/11/4
 */
public class DrawHookView extends View {
    private static final int FRAGS = 60; //帧率
    private static final int DEFAULT_WIDTH = 10; //默认线宽

    private static final int LOADING_ANGLE_LENGTH = 110; //loading动画角度跨度
    private static final int LOADING_DURATION = 1000; //loading动画转一圈的时间

    private static final int ANIM_NULL = 0;
    private static final int ANIM_LOADING = 1;
    private static final int ANIM_FINISH = 2;
    private int animType;

    private Paint paint;

    private float strokeWidth;
    //线1的x
    private float line1_x = 0;
    //线1的y轴
    private float line1_y = 0;
    //线2的x轴
    private float line2_x = 0;
    //线2的y轴
    private float line2_y = 0;

    private float startPoint1_x;
    private float startPoint1_y;
    private float endPoint1_x;
    private float endPoint1_y;
    private float startPoint2_x;
    private float startPoint2_y;

    private long startTimestamp;
    private int startAngle;

    private boolean drawHoolFinished;

    private OnAnimEndListener onAnimEndListener;

    public DrawHookView(Context context) {
        super(context);
        init();
    }

    public DrawHookView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawHookView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        /**
         * 绘制圆弧
         **/
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(0xff7dd32c);
        //设置圆弧的宽度
        strokeWidth = DEFAULT_WIDTH;
        paint.setStrokeWidth(strokeWidth);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.STROKE);
        //消除锯齿
        paint.setAntiAlias(true);
    }

    /**
     * 设置线条宽度
     * @param strokeWidth
     */
    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        paint.setStrokeWidth(strokeWidth);
    }

    /**
     * 设置线条颜色
     * @param lineColor
     */
    public void setLineColor(int lineColor) {
        paint.setColor(lineColor);
    }

    /**
     * 开始Loading动画
     */
    public void startLoadAnim() {
        if (animType != ANIM_LOADING) {
            startTimestamp = System.currentTimeMillis();
            startAngle = 0;
        }
        animType = ANIM_LOADING;
        postInvalidate();
    }

    /**
     * 开始Finish动画
     */
    public void startFinishAnim() {
        startTimestamp = System.currentTimeMillis();
        if (animType != ANIM_LOADING) {
            startAngle = 0;
        }
        line1_x = 0;
        line1_y = 0;
        line2_x = 0;
        line2_y = 0;
        drawHoolFinished = false;
        animType = ANIM_FINISH;
        postInvalidate();
    }

    /**
     * 停止并清除动画
     */
    public void clearAnim() {
        animType = ANIM_NULL;
        postInvalidate();
    }

    /**
     * 设置动画完成监听
     * @param onAnimEndListener
     */
    public void setOnAnimEndListener(OnAnimEndListener onAnimEndListener) {
        this.onAnimEndListener = onAnimEndListener;
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //获取圆心的x坐标
        int centerY = getWidth() / 2;
        int centerX = getHeight() / 2;
        //圆弧半径
        int radius = (int) (centerX > centerY ? centerX - strokeWidth : centerY - strokeWidth);

        long delta = System.currentTimeMillis() - startTimestamp;

        RectF rectF = new RectF(centerY - radius - 1, centerY - radius - 1, centerY + radius + 1, centerY + radius + 1);

        switch (animType) {
            case ANIM_NULL:
                return;
            case ANIM_LOADING: {
                float percent = (float) delta / (float) LOADING_DURATION;
                if (percent >= 1) {
                    percent -= delta / LOADING_DURATION;
                }
                startAngle = (int) (percent * 360);
                float endAngle = startAngle + LOADING_ANGLE_LENGTH;
                if (endAngle > 360) {
                    canvas.drawArc(rectF, startAngle, 360 - startAngle, false, paint);
                    canvas.drawArc(rectF, 0, endAngle - 360, false, paint);
                } else {
                    canvas.drawArc(rectF, startAngle, LOADING_ANGLE_LENGTH, false, paint);
                }
            }
                break;
            case ANIM_FINISH: {
                //计算画圈的角速度
                float angleSpeed = 360f / (float) LOADING_DURATION;
                //需要旋转的角度
                float angleDistance = 360f - LOADING_ANGLE_LENGTH;
                float drawCircleDuration = angleDistance / angleSpeed;
                float percent = (float) delta / (float) drawCircleDuration;
                if (percent > 1) {
                    //如果画完圈了，就开始画勾
                    canvas.drawArc(rectF, 0, 360, false, paint);

                    //计算画勾的线绘制速度 * 4
                    double hookSpeed = (double) (2f * Math.PI * (float) radius * (angleSpeed / 360)) * 8f;

                    Path path = new Path();

                    //画第一根线
                    startPoint1_x = centerX - radius / 2;
                    startPoint1_y = centerY;
                    endPoint1_x = centerX;
                    endPoint1_y = centerY + radius / 2;
                    if (line1_x < radius / 2 && line1_y < radius / 2) {
                        line1_x+=hookSpeed;
                        line1_y+=hookSpeed;
                    }
                    path.moveTo(startPoint1_x, startPoint1_y);
                    path.lineTo(startPoint1_x + line1_x, startPoint1_y + line1_y);
                    //画第二根线
                    if (line1_x > radius / 2 && line1_y > radius / 2) {
                        startPoint2_x = endPoint1_x;
                        startPoint2_y = endPoint1_y;

                        if (line2_x < radius / 2 && line2_y < radius) {
                            line2_x += hookSpeed;
                            line2_y += hookSpeed * 2f;
                        } else {
                            if (!drawHoolFinished) {
                                drawHoolFinished = true;
                                if (onAnimEndListener != null) {
                                    onAnimEndListener.onAnimEnd();
                                }
                            }
                        }
                        path.lineTo(startPoint2_x + line2_x, startPoint2_y - line2_y);
                    }
                    canvas.drawPath(path, paint);
                } else {
                    canvas.drawArc(rectF, startAngle, LOADING_ANGLE_LENGTH + angleDistance * percent, false, paint);

                }
            }
                break;
        }

        //刷新按照帧数刷新界面
        postInvalidateDelayed(1000 / FRAGS);
    }

    public interface OnAnimEndListener {
        public void onAnimEnd();
    }
}
